package com.srwl.mytx.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.RechargeDetailActivity;
import com.srwl.mytx.activity.WithdrawDetailActivity;
import com.srwl.mytx.domain.Recharge;
import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RechargeRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<Recharge> rechargeList;
    private final Context context;
    private boolean bol_noData;//标记是否有数据
    private boolean bol_requestError;//标记是否有数据


    public RechargeRecordAdapter(Context context, List<Recharge> rechargeList) {
        this.rechargeList = rechargeList;
        this.context = context;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        /*if (viewType == -1) {
            return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.no__data__layout, parent, false));
        }
        return new RechargeViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_recharge_record_item, parent, false));*/
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.no__data__layout, parent, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.request_error_layout, parent, false));
        }
        return new RechargeViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_recharge_record_item, parent, false));


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        /*if (rechargeList.size()==0){
            NoDataViewHolder viewHolder= (NoDataViewHolder) holder;
            viewHolder.tv_tips.setText("没有充值记录");
            return;
        }
        RechargeViewHolder viewHolder= (RechargeViewHolder) holder;
        Recharge recharge = rechargeList.get(position);
        viewHolder.tv_amount.setText(recharge.getAmount() + "");
//        String tradeStatus = "";
//        if (TextUtils.equals(recharge.getTradeStatus(), "TRADE_SUCCESS")) {
//            tradeStatus = "完成";
//        }
//        holder.tv_status.setText(tradeStatus);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        viewHolder.tv_date.setText(format.format(recharge.getGmtPayment()));*/
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            NoDataViewHolder noDataViewHolder = (NoDataViewHolder) holder;
            noDataViewHolder.tv_tips.setText("没有记录");
        } else if (viewType == Constant.ITEM_TYPE_NORMAL) {
            RechargeViewHolder viewHolder = (RechargeViewHolder) holder;
            Recharge recharge = rechargeList.get(position);
            viewHolder.tv_amount.setText(Utils.moneyFormat(recharge.getAmount()));
            viewHolder.tv_date.setText(DateUtils.dateToString(recharge.getGmtPayment(), DateUtils.FORMAT_TIMESTAMP));

            Integer status = recharge.getStatus();
            if (status != null) {
                switch (status) {
                    case Constant.RECHARGE_STATUS_INIT:
                        viewHolder.tv_status.setText("未支付");
                        viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.text_black));
                        viewHolder.tv_amount.setTextColor(context.getResources().getColor(R.color.text_black));
                        break;

                    case Constant.RECHARGE_STATUS_SUCCESS:
                        viewHolder.tv_status.setText("充值成功");
                        viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.rp_money_red_light));
                        viewHolder.tv_amount.setTextColor(context.getResources().getColor(R.color.rp_money_red_light));

                        break;

                    case Constant.RECHARGE_STATUS_ERROR:
                        viewHolder.tv_status.setText("充值失败");
                        viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.text_gray));
                        viewHolder.tv_amount.setTextColor(context.getResources().getColor(R.color.text_gray));
                        break;
                    default:
                        viewHolder.tv_status.setText("异常订单");
                        viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.text_gray));
                        viewHolder.tv_amount.setTextColor(context.getResources().getColor(R.color.text_gray));

                }
            }else {
                viewHolder.tv_status.setText("异常订单");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.text_gray));
                viewHolder.tv_amount.setTextColor(context.getResources().getColor(R.color.text_gray));
            }

            viewHolder.itemView.setOnClickListener(v -> RechargeDetailActivity.startActivity(context, recharge.getId()));
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        }
        return Constant.ITEM_TYPE_NORMAL;

    }

    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return rechargeList.size();
    }


    class RechargeViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_amount)
        TextView tv_amount;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.tv_status)
        TextView tv_status;

        public RechargeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }


}